﻿using FighttheLandOwner.GameCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FighttheLandOwner
{

    class CardGroup
    {
        public List<Card> Cards { get; set; }
        /// <summary>
        /// 整个牌堆中最大的牌
        /// </summary>
        public int CardSize { get; set; }
        public CardGrpType CardGrpType { get; set; }
        public int Value { get; set; }

        public CardGroup()
        {
            Cards = new List<Card>();
            CardSize = 0;
            CardGrpType = CardGrpType.Error;
            Value = 0;
        }

        public CardGroup(List<Card> cards)
        {
            Cards = CardManager.Arange(cards);
            if (cards.Count > 0)
                CardSize = 0;
            GetGrpTypeAndSetCardsSize();

        }

        public CardGroup(Card card)
        {
            Cards = new List<Card>();
            Cards.Add(card);
            CardGrpType = CardGrpType.单张;
            Value = card.ToInt();
        }

        /// <summary>
        /// 判断是不是单顺子
        /// </summary>
        /// <param name="cards"></param>
        /// <returns></returns>
        private bool IsShunzi(List<Card> cards)
        {
            Card p = new Card(CardType.Clubs, CardValue.A);
            int i = 0;
            foreach (var q in cards)
            {
                if (i == 0)
                {
                    p = q;
                    if (p.Value == CardValue.Joker || p.Value == CardValue.Two)
                        return false;
                    i++;
                    continue;
                }
                if (p.ToInt() != q.ToInt() + 1)
                    return false;
                p = q;
            }
            return true;
        }

        /// <summary>
        /// 判断几张牌是否相等
        /// </summary>
        /// <param name="cards"></param>
        /// <param name="cnt">判断几张</param>
        /// <returns></returns>
        private bool IsSame(List<Card> cards, int begin, int end)
        {
            bool flag = true;
            int first = cards[begin - 1].ToInt();
            for (int i = begin; i < end; i++)
            {
                if (first != cards[i].ToInt())
                {
                    flag = false;
                    break;
                }
            }
            return flag;
        }

        /// <summary>
        /// 判断是不是连对（双顺）
        /// </summary>
        /// <param name="cards"></param>
        /// <returns></returns>
        private bool IsLianDui(List<Card> cards)
        {
            foreach (var p in cards)
            {
                if (p.Value == CardValue.Two || p.Value == CardValue.Joker)
                    return false;
            }
            bool flag = true;
            for (int i = 0; i < cards.Count-1; i++)
            {
                if (flag)
                {
                    if (cards[i].ToInt() != cards[i + 1].ToInt())
                        return false;
                    flag = false;
                }
                else
                {
                    if (cards[i].ToInt() != cards[i + 1].ToInt() + 1)
                        return false;
                    flag = true;
                }
            }
            return true;
        }

        /// <summary>
        /// 判断是不是三顺
        /// </summary>
        /// <param name="cards"></param>
        /// <returns></returns>
        private bool IsSanshun(List<Card> cards)
        {
            foreach (var p in cards)
            {
                if (p.Value == CardValue.Two || p.Value == CardValue.Joker)
                    return false;
            }
            for (int i = 0; i < cards.Count; i += 3)
            {
                if (cards[i].ToInt() != cards[i].ToInt() || cards[i].ToInt() != cards[i + 2].ToInt())
                {
                    return false;
                }
                if (i + 3 < cards.Count && cards[i].ToInt() != cards[i + 3].ToInt() + 1)
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 判断是不是飞机带翅膀
        /// </summary>
        /// <param name="cards"></param>
        /// <returns></returns>
        private bool IsPlaneWithChibang(List<Card> cards)
        {
            foreach (var p in cards)
            {
                if (p.Value == CardValue.Two || p.Value == CardValue.Joker)
                    return false;
            }
            int cnt = cards.Count;
            int planecnt = cnt / 4;
            int plane = 1;
            for (int i = 0; i < cnt - 3; i++)
            {
                if (cards[i].ToInt() == cards[i + 1].ToInt() && cards[i].ToInt() == cards[i + 2].ToInt() && cards[i + 3].ToInt() + 1 == cards[i].ToInt()
                    && cards[i + 3].ToInt() == cards[i + 4].ToInt() && cards[i + 3].ToInt() == cards[i + 5].ToInt())  
                {
                    plane++;
                    i += 2;
                }
            }
            return plane == cnt;
        }

        /// <summary>
        /// 判断出牌类型并计算出牌的值
        /// </summary>
        private void GetGrpTypeAndSetCardsSize()
        {
            switch (Cards.Count)
            {
                case 0:
                    CardGrpType = CardGrpType.Error;
                    break;
                case 1:
                    CardGrpType = CardGrpType.单张;
                    CardSize = Cards[0].ToInt();
                    break;
                case 2:
                    if (Cards[0].Value == CardValue.Joker && Cards[1].Value == CardValue.Joker)
                    {
                        CardGrpType = CardGrpType.火箭;
                        CardSize = 100;
                        break;
                    }
                    if (IsSame(Cards, 1, 2))
                    {
                        CardGrpType = CardGrpType.对子;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    
                    CardGrpType = CardGrpType.Error;
                    break;
                case 3:
                    if (IsSame(Cards, 1, 3))
                    {
                        CardGrpType = CardGrpType.三张相同;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    CardGrpType = CardGrpType.Error;
                    break;
                case 4:
                    if (IsSame(Cards, 1, 4))
                    {
                        CardGrpType = CardGrpType.炸弹;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    if (IsSame(Cards, 1, 3) || IsSame(Cards, 2, 4))
                    {
                        CardGrpType = CardGrpType.三带一;
                        if(IsSame(Cards,1,3))
                        {
                            CardSize = Cards[0].ToInt();
                        }
                        else
                        {
                            CardSize = Cards[3].ToInt();
                        }
                        break;
                    }
                    CardGrpType = CardGrpType.Error;
                    break;
                case 5:
                    if (IsShunzi(Cards))
                    {
                        CardGrpType = CardGrpType.五张顺子;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    if ((IsSame(Cards, 1, 3) && IsSame(Cards, 4, 5)) || (IsSame(Cards, 1, 2) && IsSame(Cards, 3, 5)))
                    {
                        CardGrpType = CardGrpType.三带一对;
                        if(IsSame(Cards,1,3))
                        {
                            CardSize = Cards[0].ToInt();
                        }
                        else
                        {
                            CardSize = Cards[4].ToInt();
                        }
                        break;
                    }
                    CardGrpType = CardGrpType.Error;
                    break;
                case 6:
                    if (IsShunzi(Cards))
                    {
                        CardGrpType = CardGrpType.六张顺子;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    if (IsLianDui(Cards))
                    {
                        CardGrpType = CardGrpType.三连对;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    if ((IsSame(Cards, 1, 4) && IsSame(Cards, 5, 6)) ||
                                (IsSame(Cards, 1, 2) && IsSame(Cards, 3, 6)))
                    {
                        CardGrpType = CardGrpType.四带二;
                        if(IsSame(Cards,1,4))
                        {
                            CardSize = Cards[0].ToInt();
                        }
                        else
                        {
                            CardSize = Cards[5].ToInt();
                        }
                        break;
                    }
                    if (IsSanshun(Cards))
                    {
                        CardGrpType = CardGrpType.二连飞机;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    CardGrpType = CardGrpType.Error;
                    break;
                case 7:
                    if (IsShunzi(Cards))
                    {
                        CardGrpType = CardGrpType.七张顺子;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    CardGrpType = CardGrpType.Error;
                    break;
                case 8:
                    if (IsShunzi(Cards))
                    {
                        CardGrpType = CardGrpType.八张顺子;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    if (IsLianDui(Cards))
                    {
                        CardGrpType = CardGrpType.四连对;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    if (IsPlaneWithChibang(Cards))
                    {
                        CardGrpType = CardGrpType.飞机带翅膀;
                        if(IsSame(Cards,1,3))
                        {
                            CardSize = Cards[0].ToInt();
                        }
                        else if(IsSame(Cards,2,4))
                        {
                            CardSize = Cards[1].ToInt();
                        }
                        else
                        {
                            CardSize = Cards[2].ToInt();
                        }
                        break;
                    }
                    CardGrpType = CardGrpType.Error;
                    break;
                case 9:
                    if (IsShunzi(Cards))
                    {
                        CardGrpType = CardGrpType.九张顺子;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    if (IsSanshun(Cards))
                    {
                        CardGrpType = CardGrpType.三连飞机;
                        CardSize = Cards[0].ToInt();
                    }
                    CardGrpType = CardGrpType.Error;
                    break;
                case 10:
                    if (IsShunzi(Cards))
                    {
                        CardGrpType = CardGrpType.十张顺子;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    if (IsLianDui(Cards))
                    {
                        CardGrpType = CardGrpType.五连对;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    CardGrpType = CardGrpType.Error;
                    break;
                case 11:
                    if (IsShunzi(Cards))
                    {
                        CardGrpType = CardGrpType.十一张顺子;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    CardGrpType = CardGrpType.Error;
                    break;
                case 12:
                    if (IsShunzi(Cards))
                    {
                        CardGrpType = CardGrpType.十二张顺子;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    if (IsLianDui(Cards))
                    {
                        CardGrpType = CardGrpType.六连对;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    if (IsSanshun(Cards))
                    {
                        CardGrpType = CardGrpType.四连飞机;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    if (IsPlaneWithChibang(Cards))
                    {
                        CardGrpType = CardGrpType.三连飞机带翅膀;
                        if (IsSame(Cards, 1, 3))
                        {
                            CardSize = Cards[0].ToInt();
                        }
                        else if (IsSame(Cards, 2, 4)) 
                        {
                            CardSize = Cards[1].ToInt();
                        }
                        else if(IsSame(Cards,3,5))
                        {
                            CardSize = Cards[2].ToInt();
                        }
                        else
                        {
                            CardSize = Cards[3].ToInt();
                        }
                        break;
                    }
                    CardGrpType = CardGrpType.Error;
                    break;
                case 13:
                    CardGrpType = CardGrpType.Error;
                    break;
                case 14:
                    if (IsLianDui(Cards))
                    {
                        CardGrpType = CardGrpType.七连对;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    CardGrpType = CardGrpType.Error;
                    break;
                case 15:
                    if (IsSanshun(Cards))
                    {
                        CardGrpType = CardGrpType.五连飞机;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    CardGrpType = CardGrpType.Error;
                    break;
                case 16:
                    if (IsLianDui(Cards))
                    {
                        CardGrpType = CardGrpType.八连对;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    if (IsPlaneWithChibang(Cards))
                    {
                        CardGrpType = CardGrpType.四连飞机带翅膀;
                        if (IsSame(Cards, 1, 3))
                        {
                            CardSize = Cards[0].ToInt();
                        }
                        else if (IsSame(Cards, 2, 4))
                        {
                            CardSize = Cards[1].ToInt();
                        }
                        else if (IsSame(Cards, 3, 5))
                        {
                            CardSize = Cards[2].ToInt();
                        }
                        else if(IsSame(Cards,4,6))
                        {
                            CardSize = Cards[3].ToInt();
                        }
                        else
                        {
                            CardSize = Cards[4].ToInt();
                        }
                        break;
                    }
                    CardGrpType = CardGrpType.Error;
                    break;
                case 17:
                    CardGrpType = CardGrpType.Error;
                    break;
                case 18:
                    if (IsLianDui(Cards))
                    {
                        CardGrpType = CardGrpType.九连对;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    if (IsSanshun(Cards))
                    {
                        CardGrpType = CardGrpType.六连飞机;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    CardGrpType = CardGrpType.Error;
                    break;
                case 19:
                    CardGrpType = CardGrpType.Error;
                    break;
                case 20:
                    if (IsLianDui(Cards))
                    {
                        CardGrpType = CardGrpType.十连对;
                        CardSize = Cards[0].ToInt();
                        break;
                    }
                    if (IsPlaneWithChibang(Cards))
                    {
                        CardGrpType = CardGrpType.五连飞机带翅膀;
                        if (IsSame(Cards, 1, 3))
                        {
                            CardSize = Cards[0].ToInt();
                        }
                        else if (IsSame(Cards, 2, 4))
                        {
                            CardSize = Cards[1].ToInt();
                        }
                        else if (IsSame(Cards, 3, 5))
                        {
                            CardSize = Cards[2].ToInt();
                        }
                        else if (IsSame(Cards, 4, 6))
                        {
                            CardSize = Cards[3].ToInt();
                        }
                        else if(IsSame(Cards,5,6))
                        {
                            CardSize = Cards[4].ToInt();
                        }
                        else
                        {
                            CardSize = Cards[5].ToInt();
                        }
                        break;
                    }
                    CardGrpType = CardGrpType.Error;
                    break;
            }
        }

        public static bool operator>(CardGroup A, CardGroup B)
        {
            if(A.CardGrpType==B.CardGrpType)
            {
                if(A.CardSize>B.CardSize)
                    return true;
            }
            if(A.CardGrpType== CardGrpType.炸弹&&(B.CardGrpType!= CardGrpType.炸弹&&B.CardGrpType!= CardGrpType.火箭))
                return true;
            if (A.CardGrpType == CardGrpType.炸弹 && B.CardGrpType == CardGrpType.火箭)
                return false;
            if (B.CardGrpType== CardGrpType.Error) return true;
            if (A.CardGrpType == CardGrpType.火箭) return true;
            return false;
        }

        public static bool operator <(CardGroup A, CardGroup B)
        {
            return B > A;
        }
    }

    /// <summary>
    /// 出牌类型
    /// </summary>
    enum CardGrpType
    {
        单张 = 1,
        对子,
        火箭,
        三张相同,
        三带一,
        炸弹 ,
        五张顺子 ,
        三带一对,
        六张顺子,
        三连对 ,
        四带二 ,
        二连飞机 ,
        七张顺子 ,
        四连对 ,
        八张顺子 ,
        飞机带翅膀 ,
        九张顺子 ,
        三连飞机 ,
        五连对 ,
        十张顺子 ,
        十一张顺子 ,
        十二张顺子 ,
        四连飞机,
        三连飞机带翅膀,
        六连对 ,
        //没有13
        七连对,
        五连飞机 ,
        八连对 ,
        四连飞机带翅膀 ,
        //没有17
        九连对 ,
        六连飞机 ,
        //没有19
        十连对 ,
        五连飞机带翅膀,

        Error=0
    }
}
